package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.Servicecategories;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.ServicecategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/servicecategories")
public class ServicecategoriesController {

    @Autowired
    private ServicecategoriesService servicecategoriesService;

    // 添加服务类别
    @PostMapping("/add")
    public Result addServicecategories(@RequestBody Servicecategories servicecategories) {
        int result = servicecategoriesService.addServicecategories(servicecategories);
        if (result > 0) {
            return new Result(true, "添加成功");
        } else {
            return new Result(false, "添加失败");
        }
    }

    // 更新服务类别
    @PutMapping("/update")
    public Result updateServicecategories(@RequestBody Servicecategories servicecategories) {
        int result = servicecategoriesService.updateServicecategories(servicecategories);
        if (result > 0) {
            return new Result(true, "更新成功");
        } else {
            return new Result(false, "更新失败");
        }
    }

    // 根据服务类别ID删除服务类别
    @DeleteMapping("/delete/{service_id}")
    public Result deleteServicecategoriesById(@PathVariable int service_id) {
        int result = servicecategoriesService.deleteServicecategoriesById(service_id);
        if (result > 0) {
            return new Result(true, "删除成功");
        } else {
            return new Result(false, "删除失败");
        }
    }

    // 根据服务类别ID获取服务类别信息
    @GetMapping("/{service_id}")
    public Result getServicecategoriesById(@PathVariable int service_id) {
        Servicecategories servicecategories = servicecategoriesService.getServicecategoriesById(service_id);
        if (servicecategories != null) {
            return new Result(true, servicecategories);
        } else {
            return new Result(false, "未找到对应的服务类别");
        }
    }

    // 获取所有服务类别信息
    @GetMapping("/all")
    public Result getAllServicecategories() {
        List<Servicecategories> result = servicecategoriesService.getAllServicecategories();
        if (result != null && !result.isEmpty()) {
            return new Result(true, result);
        } else {
            return new Result(false, "暂无服务类别数据");
        }
    }
}
